<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    console.log(React)
    class App extends React.Component {
        // 创建ref对象用来保存真实dom对象或组件实例对象
        h1Ref=React.createRef();
        a = React.createRef();
        childRef = React.createRef();
        render() {
            console.log(this.h1Ref)
            return (
                <div>
                    {
                        /* 绑定ref对象 */
                    }
                    <h1 ref={this.h1Ref}>好好学习天天向上</h1>
                    <h2 ref={this.a}>学习使我快乐</h2>
                    <button onClick={()=>{
                        console.log(this.h1Ref)
                        /* 
                           获取ref对象中保存的实例
                        */
                        this.h1Ref.current.style.color='red';
                        this.a.current.style.color = 'blue'
                    }}>改变样式</button>
                    {
                        /* 通过createRef获取子组件实例对象 */
                    }
                    <Child ref={this.childRef} />
                    <button onClick={()=>{
                        console.log(this.childRef)
                       console.log( this.childRef.current.state.num)
                    }}>获取child组件实例</button>
                </div>
            )
        }
    }
    
    class Child extends React.Component{
        state={
            num:0
        }
        render(){
            return (
                <h1>我是子组件---{this.state.num}</h1>
            )
        }
    }
    ReactDOM.render(<App />, document.getElementById("root"));
</script>

</html>